package com.project.lk.nurseapp.remind;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.project.lk.nurseapp.R;
import com.project.lk.nurseapp.base.BaseActivity;
import com.project.lk.nurseapp.base.BaseApp;
import com.project.lk.nurseapp.db.RemindBean;
import com.project.lk.nurseapp.db.RemindBeanDao;
import com.project.lk.nurseapp.utils.AppManager;
import com.project.lk.nurseapp.utils.PopWindowManager;
import com.project.lk.nurseapp.utils.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;
/**
 * 功能介绍：
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/9
 * 修改日期：
 */
public class AddRemindActivity extends BaseActivity {


    @BindView(R.id.remind_time_tv)
    TextView remindTimeTv;
    @BindView(R.id.remind_day_tv)
    TextView remindDayTv;
    @BindView(R.id.remind_edit)
    EditText remindEdit;
    @BindView(R.id.remind_add_but)
    Button remindAddBut;
    private PopupWindow datePopupWindow;
    private View dateContentView;
    private View praentView;

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        setTime();
        setDatePopwind();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_remind;
    }




    @OnClick({R.id.back, R.id.remind_time_layout, R.id.remind_day_layout, R.id.remind_add_but})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                AppManager.getAppManager().finishActivity();
                break;
            case R.id.remind_time_layout: //选择时间
                mTimePickerView.show();
                break;
            case R.id.remind_day_layout:  //选择星期数

                PopWindowManager.showAsLocationWindow(praentView,dateContentView
                        ,datePopupWindow,R.id.date_popwind_layout);
                break;
            case R.id.remind_add_but: //添加提醒
                if (TextUtils.isEmpty(remindEdit.getText())) {
                    ToastUtils.showShortToast("请添加提醒");
                }else if (TextUtils.isEmpty(remindDayTv.getText())){
                        ToastUtils.showShortToast("请选择日期");
                }else if (TextUtils.isEmpty(remindTimeTv.getText())){
                    ToastUtils.showShortToast("请选择时间");
                }else {
                    RemindBeanDao remindBeanDao = BaseApp.getInstance().getDaoSession().getRemindBeanDao();
                    RemindBean remindBean=new RemindBean();
                    remindBean.setRemindContext(remindEdit.getText().toString());
                    remindBean.setIsOpen(false);
                    remindBean.setDateStr(remindDayTv.getText().toString());
                    remindBean.setTime(remindTimeTv.getText().toString());
                    remindBeanDao.save(remindBean);
                    AppManager.getAppManager().finishActivity();
                }
                break;
        }
    }
    List<String> dateList=new ArrayList<>();
    String[] date=new String[]{"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};
    StringBuilder dateStr=new StringBuilder();
    //设置弹窗
    private void setDatePopwind() {
        praentView = LayoutInflater.from(this).inflate(R.layout.activity_add_remind, null);
        dateContentView = LayoutInflater.from(this).inflate(R.layout.date_popwind_layout, null);
        datePopupWindow = new PopupWindow(dateContentView,
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);

        Button sureBut = (Button) dateContentView.findViewById(R.id.date_sure_but);
        Button  cancelBut = (Button) dateContentView.findViewById(R.id.date_cancel_but);
        RecyclerView dateRecyclerView = dateContentView.findViewById(R.id.date_recyclerview);
        final List<DateItemBean> list = new ArrayList<>();
        for (int i=0;i<date.length;i++){
            DateItemBean dateItemBean=new DateItemBean();
            dateItemBean.setTitle(date[i]);
            dateItemBean.setSelect(false);
            list.add(dateItemBean);
        }
        DateChooseAdapter adapter=new DateChooseAdapter(list,this);
        LinearLayoutManager manager=new LinearLayoutManager(this);
        dateRecyclerView.setAdapter(adapter);
        dateRecyclerView.setLayoutManager(manager);
        adapter.setCheckEvent(new DateChooseAdapter.ItemCheckListener() {
            @Override
            public void check(int position, boolean isChecked) {
                list.get(position).setSelect(isChecked);
            }
        });

        //点击事件
        setEvent(cancelBut, sureBut, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.date_cancel_but: //取消
                        datePopupWindow.dismiss();
                        break;
                    case R.id.date_sure_but:  //确定
                        dateStr.delete(0,dateStr.length());
                        for (int i=0;i<list.size();i++){
                            if (list.get(i).isSelect()){
                                String substring = list.get(i).getTitle().substring(2);
                                dateStr.append(substring+" ");
                            }
                        }
                        remindDayTv.setText(dateStr);
                        datePopupWindow.dismiss();
                        break;
                }

            }
        });
    }
    //设置点击事件
    private void setEvent(Button cancelBut, Button sureBut, View.OnClickListener listener) {
        cancelBut.setOnClickListener(listener);
        sureBut.setOnClickListener(listener);
    }

    private TimePickerView mTimePickerView;
    public void setTime() {
        //设置时间显示在文本上
        mTimePickerView = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {

                SimpleDateFormat format = new SimpleDateFormat("HH:mm", Locale.CHINA);
                remindTimeTv.setText(format.format(date));

            }
        }).setCancelColor(getResources().getColor(R.color.heard_background))
                .setSubmitColor(getResources().getColor(R.color.heard_background))
                .isCyclic(true)
                .setType(TimePickerView.Type.HOURS_MINS).build();
    }
}
